#include "muduo/proto/codec.h"
#include "muduo/proto/dispatcher.h"

#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpServer.h"

#include "request.pb.h"
#include <iostream>
#include <unordered_map>

class Server
{
public:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<bit::TranslateRequest> TranslateRequestPtr;
    typedef std::shared_ptr<bit::AddRequest> AddRequestPtr;
    Server(int port) : _server(&_baseloop, muduo::net::InetAddress("0.0.0.0", port),
        "Server", muduo::net::TcpServer::kNoReusePort),
        _dispatcher(std::bind(&Server::onUnknownMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
        _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))

    {
        _dispatcher.registerMessageCallback<bit::TranslateRequest>(std::bind(&Server::onTranslate, this,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<bit::AddRequest>(std::bind(&Server::onAdd, this,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _server.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _server.setConnectionCallback(std::bind(&Server::onConnection, this, std::placeholders::_1));
    }
    void strat() {
        _server.start();
        _baseloop.loop();
    }

private:
    void onConnection(const muduo::net::TcpConnectionPtr& conn)
    {
        if (conn->connected())
        {
            LOG_INFO << "新连接建立成功！";
        }
        else
        {
            LOG_INFO << "连接建立失败！";
        }
    }

    // 具体的翻译处理事务
    std::string translate(const std::string str)
    {
        std::unordered_map<std::string, std::string> hash = {
            {"hello", "你好"},
            {"Hello", "你好"},
            {"你好", "Hello"},
            {"吃了吗", "油泼面"} };
        auto res = hash.find(str);
        if (res == hash.end())
        {
            return "暂未推出该词翻译";
        }
        return res->second;
    }
    void onTranslate(const muduo::net::TcpConnectionPtr& conn, const TranslateRequestPtr& message, muduo::Timestamp)
    {
        // 1.提取message中的有效信息，也就是需要翻译的内容
        std::string req_msg = message->msg();
        // 2.进行翻译，得到结果
        std::string res_msg = translate(req_msg);
        // 3.组织protobuf的相应格式
        bit::TranslateResponse resp;
        resp.set_msg(res_msg);
        // 4.发送相应结果
        _codec.send(conn, resp);
    }
    void onAdd(const muduo::net::TcpConnectionPtr& conn, const AddRequestPtr& message, muduo::Timestamp)
    {
        // 1.提取message中的有效信息，也就是需要相加的内容
        int a = message->num1();
        int b = message->num2();
        // 2.进项相加，得到结果
        int result = a + b;
        // 3.组织protobuf的相应格式
        bit::AddResponse resp;
        resp.set_result(result);
        // 4.发送相应结果
        _codec.send(conn, resp);
    }

    void onUnknownMessage(const muduo::net::TcpConnectionPtr& conn, const MessagePtr& message, muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }

private:
    muduo::net::EventLoop _baseloop;
    muduo::net::TcpServer _server;  // 服务器对象
    ProtobufDispatcher _dispatcher; // 请求分发器对象--要向其中注册请求处理函数
    ProtobufCodec _codec;           // protobuf协议处理器--针对收到的请求数据进行protobuf协议处理
};


int main() {
    Server server(8080);
    server.strat();
    return 0;
}